package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.ProcessStockSelloutBean;
import com.ymatou.iapi.livebuyer.service.ProcessStockSelloutCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
/**
*处理库存售罄消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ProcessStockSellout{
	private static ProcessStockSelloutBean processstockselloutBean;
	private static ProcessStockSelloutCall processstockselloutCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("处理库存售罄消息");
	}

	@Before
	public void caseUp() {
		processstockselloutBean=new ProcessStockSelloutBean();
		processstockselloutCall=new ProcessStockSelloutCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(processstockselloutCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessStockSellout_001() {
		Logger.start(true,"处理规格库存售罄1消息成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			CreateSellerLiveBean liveb = LiveRoomCallService.initCreatesellerliveBean(uinfo);
			liveb.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("test",liveb.getLiveDto().getUserId()));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(liveb);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			Thread.sleep(20000);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			processstockselloutBean.setRequestId(reqid);
			processstockselloutBean.setProductId(pid);
			processstockselloutBean.setStockType(1);
			processstockselloutCall.setData(processstockselloutBean);
			processstockselloutCall.callService();
			Logger.verifyEquals("ok", processstockselloutCall.getReturnData(),"验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(processstockselloutBean.getRequestId().toString())).filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(1,filter.size(),"验证日志");
			//TODO 需要查看服务器日志验证消息发送记录
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessStockSellout_002() {
		Logger.start(true,"处理活动库存售罄2消息成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			CreateSellerLiveBean liveb = LiveRoomCallService.initCreatesellerliveBean(uinfo);
			liveb.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("test",liveb.getLiveDto().getUserId()));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(liveb);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			processstockselloutBean.setProductId(pid);
			processstockselloutBean.setStockType(2);
			processstockselloutCall.setData(processstockselloutBean);
			processstockselloutCall.callService();
			Logger.verifyEquals("ok", processstockselloutCall.getReturnData(),"验证返回");
			//TODO 需要查看服务器日志验证消息发送记录
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProcessStockSellout_003() {
		Logger.start(true,"处理非直播商品消息成功");
		try {
			processstockselloutBean.setProductId(TestDataManager.getRandomUUID().toString());
			processstockselloutBean.setStockType(2);
			processstockselloutCall.setData(processstockselloutBean);
			processstockselloutCall.callService();
			Logger.verifyEquals("ok", processstockselloutCall.getReturnData(),"验证返回");
			//TODO 需要查看服务器日志验证消息发送记录
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessStockSellout_004() {
		Logger.start(true,"处理规格库存售罄1消息成功、计数器+1");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			CreateSellerLiveBean liveb = LiveRoomCallService.initCreatesellerliveBean(uinfo);
			liveb.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("test",liveb.getLiveDto().getUserId()));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(liveb);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			Thread.sleep(20000);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			processstockselloutBean.setRequestId(reqid);
			processstockselloutBean.setProductId(pid);
			processstockselloutBean.setStockType(1);
			processstockselloutCall.setData(processstockselloutBean);

			Integer counter = CounterCallService.getSingleValueByLiveId(createlive.getId());
			processstockselloutCall.callService();	
			Integer counter1 = CounterCallService.getSingleValueByLiveId(createlive.getId());
			Logger.verifyEquals(counter+1, counter1, "添加商品之后，计数器+1");
			
			Logger.verifyEquals("ok", processstockselloutCall.getReturnData(),"验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(processstockselloutBean.getRequestId().toString())).filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(1,filter.size(),"验证日志");
			//TODO 需要查看服务器日志验证消息发送记录
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}